﻿//房间界面
package com.victim.snake.graphics
{
	
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.CheckBox;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import fl.controls.ComboBox;
	
	import com.victim.snake.graphics.roomClass.ShowPlayerSprite;
	import com.victim.snake.graphics.roomClass.RoomChat;
	import com.victim.snake.user.User;
	import com.victim.snake.player.Player;
	import com.victim.snake.user.ProcessMsg;
	import com.victim.snake.user.Msg;	
	import com.victim.snake.util.Loader;
	import com.victim.snake.graphics.roomClass.ImageLoaderEvent;
	import com.victim.snake.graphics.roomClass.ImageLoader;
	import com.victim.snake.room.RoomBo;
	import com.victim.snake.room.RoomEvent;
	import com.victim.snake.room.Room;
	import com.victim.snake.graphics.roomClass.ShowSnakeStyleSprite;
	import com.victim.snake.snake.SnakeStyle;
	import com.victim.snake.snake.SnakeStyleEvent;
	import com.victim.snake.player.PlayerBo;
	import com.victim.snake.player.PlayerEvent;
	import com.victim.snake.map.Map;
	import com.victim.snake.map.MapEvent;
	import com.victim.snake.util.Image;
	import com.victim.snake.style.TextStyle;
	
	
	
	
	

	//房间界面
	public class RoomSprite extends Sprite
	{
		static var imageLoader:ImageLoader=new ImageLoader();	//房间界面资源下载
		
		
		var room:Room=null;
		var startGameBtn:Button=null;
		var exitBtn:Button=null;
		
		var groupRedBtn:Button=null;
		var groupGreenBtn:Button=null;
		var groupYellowBtn:Button=null;
		var groupBlueBtn:Button=null;
		
		var chatSprite:RoomChat=null;		
		//var addComputerBtn:Button=null;
		var showPlayerSprites:Array=null;
		var showSnakeStyleSprites:Array=null;
		var autoReadyCheckBox:CheckBox=null;	//自动准备选择框		
		public var mapNameList:ComboBox=new ComboBox();	//地图选择下拉菜单
		public var mapNameText:Label=new Label();	//地图文本标签
		var map:Map=null;		
		var previewMap:Sprite=null;	//地图缩略图		
		public var modeList:ComboBox=new ComboBox();	//游戏模式选择下拉框
		public var modeText:Label=new Label();	//游戏模式文本标签
		public var modeCounterList:ComboBox=new ComboBox();	//模式记数器下拉框
		public var modeCounterText:Label=new Label();	//模式记数器文本标签
		

		public function RoomSprite(){
			
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.RoomBackgroundIsLoader,roomBackgroundLoaderEvent);			//背景图片下载完成监听			
			RoomSprite.imageLoader.roomBackgroundLoader();	//背景图片下载
			
			//向服务器发送加载所有蛇类型属性消息
			var ba:ByteArray=new ByteArray();
			ba.writeByte(18);			//消息类型;
			ba.writeShort(0);			//消息内容的长度;
			ProcessMsg.socketSend(ba);
			ba.clear();
						
			//开始按钮
			startGameBtn=new Button();
			startGameBtn.x=550;
			startGameBtn.y=403;						
			this.addChild(startGameBtn);			
						
			//退出按钮
			exitBtn=new Button();
			exitBtn.label="";
			exitBtn.x=728;
			exitBtn.y=457;
			exitBtn.width=66;
			exitBtn.height=17;
			exitBtn.addEventListener(MouseEvent.CLICK,exitRoom);
			this.addChild(exitBtn);
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.LeaveUpButtonLoader,roomLeaveUpButtonLoaderEvent);			//退出按钮下载完成监听
			RoomSprite.imageLoader.leaveUpButtonLoader();	//下载退出按钮
			
			//自动准备选择框
			this.autoReadyCheckBox=new CheckBox();
			this.autoReadyCheckBox.label="";
			this.autoReadyCheckBox.x=716;
			this.autoReadyCheckBox.y=418;
			this.autoReadyCheckBox.addEventListener(Event.CHANGE,autoReadyCheckBoxChangeEvent);
			User.getPlayer().addEventListener(PlayerEvent.AutoReadyChange,playerAutoReadyChangeEvent);
			this.addChild(autoReadyCheckBox);
			
			
			//红队按钮
			this.groupRedBtn=new Button();
			this.groupRedBtn.label="";
			this.groupRedBtn.width=66;
			this.groupRedBtn.height=26;
			this.groupRedBtn.x=487;
			this.groupRedBtn.y=209;
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupRedUp,roomGroupRedUpButtonLoaderEvent);//红队up按钮下载完成监听
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupRedOver,roomGroupRedOverButtonLoaderEvent);//红队over按钮下载完成监听
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupRedDown,roomGroupRedDownButtonLoaderEvent);//红队Down按钮下载完成监听
			RoomSprite.imageLoader.groupRedUpButtonLoader();
			RoomSprite.imageLoader.groupRedOverButtonLoader();
			RoomSprite.imageLoader.groupRedDownButtonLoader();
			this.groupRedBtn.addEventListener(MouseEvent.CLICK,groupRedBtnClickEvent);
			this.addChild(this.groupRedBtn);
			//绿队按钮
			this.groupGreenBtn=new Button();
			this.groupGreenBtn.label="";
			this.groupGreenBtn.width=66;
			this.groupGreenBtn.height=26;
			this.groupGreenBtn.x=560;
			this.groupGreenBtn.y=209;
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupGreenUp,roomGroupGreenUpButtonLoaderEvent);//绿队up按钮下载完成监听
			RoomSprite.imageLoader.groupGreenUpButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupGreenOver,roomGroupGreenOverButtonLoaderEvent);//绿队over按钮下载完成监听
			RoomSprite.imageLoader.groupGreenOverButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupGreenDown,roomGroupGreenDownButtonLoaderEvent);//绿队Down按钮下载完成监听
			RoomSprite.imageLoader.groupGreenDownButtonLoader();
			this.groupGreenBtn.addEventListener(MouseEvent.CLICK,groupGreenBtnClickEvent);
			this.addChild(this.groupGreenBtn);
			//黄队按钮
			this.groupYellowBtn=new Button();
			this.groupYellowBtn.label="";
			this.groupYellowBtn.width=66;
			this.groupYellowBtn.height=26;
			this.groupYellowBtn.x=706;
			this.groupYellowBtn.y=209;
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupYellowUp,roomGroupYellowUpButtonLoaderEvent);//黄队up按钮下载完成监听
			RoomSprite.imageLoader.groupYellowUpButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupYellowOver,roomGroupYellowOverButtonLoaderEvent);//黄队over按钮下载完成监听
			RoomSprite.imageLoader.groupYellowOverButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupYellowDown,roomGroupYellowDownButtonLoaderEvent);//黄队Down按钮下载完成监听
			RoomSprite.imageLoader.groupYellowDownButtonLoader();
			this.groupYellowBtn.addEventListener(MouseEvent.CLICK,groupYellowBtnClickEvent);
			this.addChild(this.groupYellowBtn);
			//蓝队按钮
			this.groupBlueBtn=new Button();
			this.groupBlueBtn.label="";
			this.groupBlueBtn.width=66;
			this.groupBlueBtn.height=26;
			this.groupBlueBtn.x=633;
			this.groupBlueBtn.y=209;
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupBlueUp,roomGroupBlueUpButtonLoaderEvent);//蓝队up按钮下载完成监听
			RoomSprite.imageLoader.groupBlueUpButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupBlueOver,roomGroupBlueOverButtonLoaderEvent);//蓝队over按钮下载完成监听
			RoomSprite.imageLoader.groupBlueOverButtonLoader();
			RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.GroupBlueDown,roomGroupBlueDownButtonLoaderEvent);//蓝队Down按钮下载完成监听
			RoomSprite.imageLoader.groupBlueDownButtonLoader();
			this.groupBlueBtn.addEventListener(MouseEvent.CLICK,groupBlueBtnClickEvent);
			this.addChild(this.groupBlueBtn);
			
			//聊天窗口
			this.chatSprite=new RoomChat(394);
			this.chatSprite.x=22;
			this.chatSprite.y=318;
			User.setChatSprite(this.chatSprite);
			this.addChild(this.chatSprite);
			
			//添加电脑
			/*this.addComputerBtn=new Button();
			this.addComputerBtn.label="添加电脑";
			this.addComputerBtn.x=260;
			this.addComputerBtn.y=440;
			this.addComputerBtn.addEventListener(MouseEvent.CLICK,addComputerBtnClick);			*/
			
			
			
			//地图选择下拉菜单及内容文本
			var mapLable:Label=new Label();
			mapLable.text="地图:";
			mapLable.x=642;
			mapLable.y=266;
			mapLable.setStyle("textFormat",TextStyle.getRoomSpriteMapLableStyle());
			this.addChild(mapLable);
			mapNameList.x=682;
			mapNameList.y=264;
			mapNameList.width=90;			
			mapNameList.addEventListener(flash.events.Event.CHANGE,mapNameListChangeEvent);
			this.addChild(mapNameList);			
			mapNameText.text="";
			mapNameText.x=682;
			mapNameText.y=264
			mapNameText.setStyle("textFormat",TextStyle.getRoomSpriteMapLableStyle());
			this.addChild(mapNameText);
			
			
			//选择游戏模式
			var modeLable:Label=new Label();
			modeLable.text="模式:";
			modeLable.x=642;
			modeLable.y=296;
			modeLable.setStyle("textFormat",TextStyle.getRoomSpriteModeLableStyle());
			this.addChild(modeLable);
			this.modeList.addItemAt({label:"吃货模式",data:0},0);
			this.modeList.addItemAt({label:"死亡模式",data:1},1);
			modeList.x=682;
			modeList.y=294;
			modeList.width=90;
			modeList.addEventListener(Event.CHANGE,modeListChangeEvent);
			this.addChild(modeList);
			modeText.x=682;
			modeText.y=294;
			this.addChild(modeText);
			
			var modeCounterLable:Label=new Label();	//模式记数
			modeCounterLable.text="模式记数:";
			modeCounterLable.x=642;
			modeCounterLable.y=326;
			modeCounterLable.setStyle("textFormat",TextStyle.getRoomSpriteModeCounterLableStyle());
			this.addChild(modeCounterLable);
			this.modeCounterList.x=712;
			this.modeCounterList.y=326;
			this.modeCounterList.width=60;
			modeCounterList.addEventListener(Event.CHANGE,modeCounterListChangeEvent);
			this.addChild(this.modeCounterList);
			this.modeCounterText.x=712
			this.modeCounterText.y=326;
			this.addChild(this.modeCounterText);
			
			showPlayerSprites=new Array();	//显示用户用的8个显示用户框数组
			//添加8个显示用户框到房间界面
			for (var i:int=0;i<8;i++){
				var showPlayerSprite:ShowPlayerSprite=new ShowPlayerSprite();
				showPlayerSprites[i]=showPlayerSprite;
				//设置位置
				/*
				if(i<4){					
					showPlayerSprite.x=18+(i*100)+((i+1)*6);
					showPlayerSprite.y=41;
				}else{
					showPlayerSprite.x=40+(i-4)*120;
					showPlayerSprite.y=230;
				}
				*/
				
				//设置这8个显示用户框的位置
				switch(i){
					case 0:
						showPlayerSprite.x=25;
						showPlayerSprite.y=48;
						break;
					case 1:
						showPlayerSprite.x=131;
						showPlayerSprite.y=48;
						break;
					case 2:
						showPlayerSprite.x=237;
						showPlayerSprite.y=48;
						break;
					case 3:
						showPlayerSprite.x=343;
						showPlayerSprite.y=48;
						break;
					case 4:
						showPlayerSprite.x=25;
						showPlayerSprite.y=171;
						break;
					case 5:
						showPlayerSprite.x=131;
						showPlayerSprite.y=171;
						break;
					case 6:
						showPlayerSprite.x=237;
						showPlayerSprite.y=171;
						break;
					case 7:
						showPlayerSprite.x=343;
						showPlayerSprite.y=171;
						break;					
						
				}
				
				this.addChildAt(showPlayerSprite,this.numChildren);				
			}	
			
			
			this.showSnakeStyleSprites = new Array();	//创建显示蛇样式框的数组
			for(var j:int=0;j<9;j++){
				this.showSnakeStyleSprites[j]=new ShowSnakeStyleSprite();
				this.addChild(this.showSnakeStyleSprites[j]);
				
				switch(j){
					case 0:
						showSnakeStyleSprites[j].x=484;
						showSnakeStyleSprites[j].y=22;
						break;
					case 1:
						showSnakeStyleSprites[j].x=583;
						showSnakeStyleSprites[j].y=22;
						break;
					case 2:
						showSnakeStyleSprites[j].x=681;
						showSnakeStyleSprites[j].y=22;
						break;
					case 3:
						showSnakeStyleSprites[j].x=484;
						showSnakeStyleSprites[j].y=78;
						break;
					case 4:
						showSnakeStyleSprites[j].x=583;
						showSnakeStyleSprites[j].y=78;
						break;
					case 5:
						showSnakeStyleSprites[j].x=681;
						showSnakeStyleSprites[j].y=78;
						break;
					case 6:
						showSnakeStyleSprites[j].x=484;
						showSnakeStyleSprites[j].y=134;
						break;
					case 7:
						showSnakeStyleSprites[j].x=583;
						showSnakeStyleSprites[j].y=134;
						break;
					case 8:
						showSnakeStyleSprites[j].x=681;
						showSnakeStyleSprites[j].y=134;
						break;
						
				}
			}
			
			this.refreshState();	//更新房间状态（按钮是否显示或可用等）
		}
		
		//服务器发来消息27(服务器发来的各个地图属性)后调用
		public function addMapItem(map:Map):void{
			this.mapNameList.addItem({label:map.getCName(),data:map.getName()});
		}
		
		//按模式设置模式记录器的下拉列表
		private function setModeCounterListByMode():void{
			this.modeCounterList.removeAll();
			switch(this.room.getGameMode()){
				case 0:	//吃货模式
					this.modeCounterList.addItem({label:50,data:50});
					this.modeCounterList.addItem({label:80,data:85});
					this.modeCounterList.addItem({label:100,data:100});
					this.modeCounterList.addItem({label:150,data:150});
					this.modeCounterList.addItem({label:200,data:200});
					this.modeCounterList.selectedIndex=2;
					this.modeCounterText.text="100";
					break;
				case 1:	//死亡模式
					this.modeCounterList.addItem({label:1,data:1});
					this.modeCounterList.addItem({label:10,data:10});
					this.modeCounterList.addItem({label:20,data:20});
					this.modeCounterList.addItem({label:40,data:40});
					this.modeCounterList.selectedIndex=2;
					this.modeCounterText.text="20";
					break;
				default:
			}
		}
		
		//房间的地图改变事件
		public function changeMap(e:RoomEvent):void{
			this.map=e.room.getMap();
			this.map.addEventListener(MapEvent.IsLoader,previewImageLoaderEvent);
			map.load();
			
			//设置地图选择列表
			var o:Object=null;
			for(var i:int=0;i<this.mapNameList.length;i++){
				o=this.mapNameList.getItemAt(i);
				if(String((o["data"]))==this.map.getName()){
					this.mapNameList.selectedItem=o;
				}
			}
			this.mapNameText.text=this.map.getCName();	//设置地图名称显示文本
		}
		
		//地图选择列表改变事件
		public function mapNameListChangeEvent(e:Event):void{
			//删除之前的地图预览
			if(this.previewMap!=null){				
				this.removeChild(previewMap);
				this.previewMap=null;
			}
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(23,mapNameList.selectedItem.data));			
			
		}
		
		//模式计数器改变事件
		public function modeCounterListChangeEvent(e:Event):void{
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(25,this.modeCounterList.selectedItem.data));						
		}
		
		
		//显示地图预览
		function previewImageLoaderEvent(event:MapEvent):void{
			map.removeEventListener(MapEvent.IsLoader,previewImageLoaderEvent);
			this.map=event.map;
			this.previewMap=Image.getRoundImage(event.map.getPreviewImage(),30);
			this.previewMap.width=152;
			this.previewMap.height=112;
			this.previewMap.x=486;
			this.previewMap.y=264;
			//加载地图预览图到画面			
			this.addChild(this.previewMap);
		}
		
		//自动准备选择框发生改变事件
		public function autoReadyCheckBoxChangeEvent(e:Event){
			PlayerBo.sendAutoReadyMsg(User.getPlayer(),this.autoReadyCheckBox.selected);
		}
		
		//玩家自动准备状态发生改变时调用方法
		public function playerAutoReadyChangeEvent(e:PlayerEvent){
			this.autoReadyCheckBox.selected=e.player.getAutoReady();
		}
		
		//修改游戏模式下拉菜单事件
		public function modeListChangeEvent(e:Event):void{
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(24,this.modeList.selectedItem["data"]));
		}
		
		//设置游戏模式改变事件
		public function changeGameMode(e:RoomEvent):void{
			this.modeList.selectedIndex=e.room.getGameMode();
			this.setModeText();		//改变游戏模式显示文本
			this.setModeCounterListByMode();	//更新游戏计数器设置
		}
		
		//设置游戏模式计数器改变事件
		public function changeModeCounter(e:RoomEvent):void{
			setModeCounterSelectedItem(e.room.getModeCounter());
		}
		
		public function setModeCounterSelectedItem(modeCounterValue:int){
			for(var i:int=0;i<this.modeCounterList.length;i++){
				if(this.modeCounterList.getItemAt(i).data==modeCounterValue){
					this.modeCounterList.selectedIndex=i;
					this.modeCounterText.text=modeCounterValue+"";
				}
			}
		}
		
		
		//重新设置房间（加入新房间时调用）
		public function reSet():void{
			//设置新房间数据类
			this.addRoomDate(User.getRoom());
			//按房主或非房主加载开始按钮或(取消)准备按钮
			this.loaderStartOrReadyButton();
			//按房主或非房主显示地图设置菜单是否可用(包括地图选择，模式选择)
			showModeSet();
			//显示用户
			var players:Array=room.getPlayers();	//取得房间用户			
			var showPlayerSprite:ShowPlayerSprite=null;
			for (var i:int=0;i<8;i++){
				if(players[i]!=null){
					var player:Player=Player(players[i]);
					showPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[i]);
					showPlayerSprite.setPlayer(player);
				}				
			}
			
			//显示地图预览
			this.map=User.getRoom().getMap();
			//trace(map.getCName());
			this.map.addEventListener(MapEvent.IsLoader,previewImageLoaderEvent);
			this.map.load();
			
			//设置地图选择下拉框及地图文本框
			var o:Object=null;
			this.mapNameList.removeAll();
			for each(var m:Map in Map.getMaps()){
				o={label:m.getCName(),data:m.getName()};
				mapNameList.addItem(o);
				if(m.getName()==this.map.getName()){
					mapNameList.selectedItem=o;
				}
			}
			this.mapNameText.text=this.map.getCName();
			
			//设置房间模式			
			this.modeList.selectedIndex=this.room.getGameMode();
			//设置房间模式文本框
			setModeText();
			
			//设置房间模式记数器
			this.setModeCounterListByMode();
			//设置房间模式记数器文本框
			this.setModeCounterText();
			//设置房间模式记数器默认选项
			this.setModeCounterSelectedItem(this.room.getModeCounter());
			
			//如果玩家为自动准备,向服务器发送准备消息
			if(User.getPlayer().getAutoReady()){
				PlayerBo.sendReadyOrUnReadyMsg(User.getPlayer());
			}
		}
		
		//设置房间数据类
		public function addRoomDate(room:Room):void{
			this.room=room;
			//监听房间加入新玩家事件
			room.addEventListener(RoomEvent.AddPlayer,addPlayerEvent);
			//监听玩家退出房间事件
			room.addEventListener(RoomEvent.ExitPlayer,exitPlayerEvent);
			//监听房间玩家状态改变为准备
			room.addEventListener(RoomEvent.PlayerReady,playerReadyEvent);
			//监听房间玩家状态改变为取消准备
			room.addEventListener(RoomEvent.PlayerUnReady,playerUnReadyEvent);
			//监听房间设置房主事件
			room.addEventListener(RoomEvent.SetMaster,setMasterEvent);
			//监听当前房间地图改变事件
			room.addEventListener(RoomEvent.ChangeMap,changeMap);
			//监听当前房间游戏模式改变事件
			room.addEventListener(RoomEvent.ChangeGameMode,changeGameMode);
			//监听当前房间游戏模式计数器改变事件
			room.addEventListener(RoomEvent.ChangeModeCounter,changeModeCounter);
		}
		
		//初始化房间数据类
		public function init():void{
			this.initShowPlayerSprites();	//初始化8个显示用户框
			//如果有之前的房间数据类,删除对之前房间数据类的监听
			if(this.room!=null){
				//删除监听房间加入新玩家事件
				room.removeEventListener(RoomEvent.AddPlayer,addPlayerEvent);
				//删除监听玩家退出房间事件
				room.removeEventListener(RoomEvent.ExitPlayer,exitPlayerEvent);
				//删除监听房间玩家状态改变为准备
				room.removeEventListener(RoomEvent.PlayerReady,playerReadyEvent);
				//删除监听房间玩家状态改变为取消准备
				room.removeEventListener(RoomEvent.PlayerUnReady,playerUnReadyEvent);
				//删除监听房间设置房主事件
				room.removeEventListener(RoomEvent.SetMaster,setMasterEvent);
				//删除当前房间地图改变事件
				room.removeEventListener(RoomEvent.ChangeMap,changeMap);
				//监听当前房间游戏模式改变事件
				room.removeEventListener(RoomEvent.ChangeGameMode,changeGameMode);
			}
			this.room=null;
		}
		
		//初始化8个显示用户框
		private function initShowPlayerSprites():void{
			var showPlayerSprite:ShowPlayerSprite=null;
			for(var i:int=0;i<8;i++){
				showPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[i]);
				showPlayerSprite.init();
			}
		}
		
		//按房主或非房主显示地图设置菜单是否可用(包括地图选择，模式选择)
		private function showModeSet(){
			//如果当前玩家是房主
			if(User.getPlayer().getUid()==this.room.getMaster().getUid()){
				this.mapNameList.visible=true;
				this.modeList.visible=true;
				this.modeCounterList.visible=true;
				this.mapNameText.visible=false;
				this.modeText.visible=false;
				this.modeCounterText.visible=false;
			}else{	//如果当前玩家不是房主
				this.mapNameList.visible=false;
				this.modeList.visible=false;
				this.modeCounterList.visible=false;
				this.mapNameText.visible=true;
				this.modeText.visible=true;
				this.modeCounterText.visible=true;
			}
		}
		
		//按房间数据设置房间模式文本框
		private function setModeText():void{
			switch(this.room.getGameMode()){
				case 0:
					this.modeText.text="吃货模式";
					break;
				case 1:
					this.modeText.text="死亡模式";
					break;
				default:
					this.modeText.text="";
					break;
			}		
		}
		
		//设置房间模式记数器文本框
		private function setModeCounterText():void{
			this.modeCounterText.text=this.room.getModeCounter()+"";
		}
		
		
		//当服务器发来24消息代表的蛇样式资源下载完成后调用，该方法将下载完成的蛇样式显示在房间界面上
		public function showSnakeStyleEvent(e:SnakeStyleEvent):void{
			//trace("样式下载完成"+e.snakeStyle.getName());
			this.removeEventListener(SnakeStyleEvent.IsLoaded,this.showSnakeStyleEvent);
			this.showSnakeStyle(e.snakeStyle);
		}
		
		//按蛇ID显示蛇样式到房间界面相应位置
		public function showSnakeStyle(snakeStyle:SnakeStyle):void{
			/*for(var i:int=0;i<9;i++){
				var showSnakeStyleSprite:ShowSnakeStyleSprite= ShowSnakeStyleSprite(this.showSnakeStyleSprites[i]);
				if(showSnakeStyleSprite.getSnakeStyle()==null){
					trace(i);
					showSnakeStyleSprite.setSnakeStyle(snakeStyle);
				}
			}*/
			
			var showSnakeStyleSprite:ShowSnakeStyleSprite= ShowSnakeStyleSprite(this.showSnakeStyleSprites[snakeStyle.getId()]);
			if(showSnakeStyleSprite.getSnakeStyle()==null){
				showSnakeStyleSprite.setSnakeStyle(snakeStyle);
			}
		}
		
		//接收到room的设置房主事件，如果新房主为当前玩家，将准备按钮改为开始按钮
		private function setMasterEvent(e:RoomEvent):void{
			this.loaderStartOrReadyButton();	//按房主或非房主加载开始按钮或(取消)准备按钮
			var sps:ShowPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[e.playerSite]);
			sps.setPlayer(e.player);
			//trace(e.player==User.getPlayer());
		}
		
		//接收到room的新用户加入事件，按用户在room中的位置显示到指定showPlayerSprite中
		private function addPlayerEvent(e:RoomEvent):void{
			var shs:ShowPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[e.playerSite]);
			shs.setPlayer(e.player);
		}
		
		//接收到room的玩家退出事件，按退出用户在room中的位置初使化指定showPlayerSprite
		private function exitPlayerEvent(e:RoomEvent):void{
			var shs:ShowPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[e.playerSite]);
			shs.init();
		}
		
		//房间玩家状态改变为准备事件
		private function playerReadyEvent(event:RoomEvent):void{
			//trace(event.player==User.getPlayer());
			if(event.player==User.getPlayer()){
				this.startGameBtn.setStyle("icon",RoomSprite.imageLoader.unReadyUpButton);
			}			
		}
		
		//房间玩家状态改变为取消准备事件
		private function playerUnReadyEvent(event:RoomEvent):void{
			//trace(event.player==User.getPlayer());
			if(event.player==User.getPlayer()){
				this.startGameBtn.setStyle("icon",RoomSprite.imageLoader.readyUpButton);
			}			
		}
		
		
		//按玩家ID取得玩家的showPlayerSprite
		public function getShowPlayerSpriteByPlayerId(playerId:int):ShowPlayerSprite{
			for(var i:int=0;i<this.showPlayerSprites.length;i++){
				var showPlayerSprite:ShowPlayerSprite=ShowPlayerSprite(this.showPlayerSprites[i])
				if(showPlayerSprite.player.getUid()==playerId){
					return showPlayerSprite;
				}
			}
			return null;
		}
		
		//按玩家取得玩家的showPlayerSprite
		public function getShowPlayerSpriteByPlayer(player:Player):ShowPlayerSprite{ 
			return this.getShowPlayerSpriteByPlayerId(player.getUid());
		}
		
		
		//加载开始或(取消)准备状态按钮图片
		public function loaderStartOrReadyButton():void{
			//trace(User.getPlayer()+";"+User.getRoom());
			if(User.getPlayer().getUid()==User.getRoom().getMaster().getUid()){
				RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.RoomStartUpButtonIsLoader,RoomStartUpButtonLoaderEvent);	//开始按钮图片下载监听
				RoomSprite.imageLoader.startUpButtonLoader();	//开始按钮图片下载
			}else{
				RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.RoomReadyUpButtonIsLoader,RoomReadyUpButtonLoaderEvent);	//准备按钮图片下载监听
				RoomSprite.imageLoader.addEventListener(ImageLoaderEvent.RoomUnReadyUpButtonIsLoader,RoomUnReadyUpButtonLoaderEvent);	//(取消准备)按钮图片下载监听
				RoomSprite.imageLoader.readyUpButtonLoader();	//准备按钮图片下载
				RoomSprite.imageLoader.unReadyUpButtonLoader();	//(取消准备)按钮图片下载
			}
		}
		
		
		//设置退出按钮
		public function roomLeaveUpButtonLoaderEvent(event:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.LeaveUpButtonLoader,roomLeaveUpButtonLoaderEvent);			
			this.exitBtn.setStyle("upSkin",imageLoader.leaveUpButton);
		}
		//设置红队按钮平时状态
		public function roomGroupRedUpButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupRedUp,roomGroupRedUpButtonLoaderEvent);//删除红队按钮下载完成监听
			this.groupRedBtn.setStyle("upSkin",imageLoader.groupRedUp);
		}
		//设置红队按钮over时状态
		public function roomGroupRedOverButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupRedUp,roomGroupRedOverButtonLoaderEvent);//删除红队按钮下载完成监听
			this.groupRedBtn.setStyle("overSkin",imageLoader.groupRedOver);
		}
		//设置红队按钮down时状态
		public function roomGroupRedDownButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupRedDown,roomGroupRedDownButtonLoaderEvent);//删除红队按钮下载完成监听
			this.groupRedBtn.setStyle("downSkin",imageLoader.groupRedDown);
		}
		//设置绿队按钮平时状态
		public function roomGroupGreenUpButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupGreenUp,roomGroupGreenUpButtonLoaderEvent);//删除绿队按钮下载完成监听
			this.groupGreenBtn.setStyle("upSkin",imageLoader.groupGreenUp);
		}
		//设置绿队按钮over状态
		public function roomGroupGreenOverButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupGreenOver,roomGroupGreenOverButtonLoaderEvent);//删除绿队按钮下载完成监听
			this.groupGreenBtn.setStyle("overSkin",imageLoader.groupGreenOver);
		}
		//设置绿队按钮down状态
		public function roomGroupGreenDownButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupGreenDown,roomGroupGreenDownButtonLoaderEvent);//删除绿队按钮下载完成监听
			this.groupGreenBtn.setStyle("downSkin",imageLoader.groupGreenDown);
		}
		//设置黄队按钮平时状态
		public function roomGroupYellowUpButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupYellowUp,roomGroupYellowUpButtonLoaderEvent);//删除黄队按钮下载完成监听
			this.groupYellowBtn.setStyle("upSkin",imageLoader.groupYellowUp);
		}
		//设置绿黄队按钮over状态
		public function roomGroupYellowOverButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupYellowOver,roomGroupYellowOverButtonLoaderEvent);//删除黄队按钮下载完成监听
			this.groupYellowBtn.setStyle("overSkin",imageLoader.groupYellowOver);
		}
		//设置黄队按钮down状态
		public function roomGroupYellowDownButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupYellowDown,roomGroupYellowDownButtonLoaderEvent);//删除黄队按钮下载完成监听
			this.groupYellowBtn.setStyle("downSkin",imageLoader.groupYellowDown);
		}
		//设置蓝队按钮平时状态
		public function roomGroupBlueUpButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupBlueUp,roomGroupBlueUpButtonLoaderEvent);//删除蓝队按钮下载完成监听
			this.groupBlueBtn.setStyle("upSkin",imageLoader.groupBlueUp);
		}
		//设置蓝黄队按钮over状态
		public function roomGroupBlueOverButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupBlueOver,roomGroupBlueOverButtonLoaderEvent);//删除蓝队按钮下载完成监听
			this.groupBlueBtn.setStyle("overSkin",imageLoader.groupBlueOver);
		}
		//设置蓝队按钮down状态
		public function roomGroupBlueDownButtonLoaderEvent(e:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.GroupBlueDown,roomGroupBlueDownButtonLoaderEvent);//删除蓝队按钮下载完成监听
			this.groupBlueBtn.setStyle("downSkin",imageLoader.groupBlueDown);
		}
		
		//设置背景图
		public function roomBackgroundLoaderEvent(event:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.RoomBackgroundIsLoader,roomBackgroundLoaderEvent);			
			this.addChildAt(RoomSprite.imageLoader.roomBackground,0);
		}
		//设置开始按钮
		public function RoomStartUpButtonLoaderEvent(event:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.RoomStartUpButtonIsLoader,RoomStartUpButtonLoaderEvent);	//删除图片下载监听
			this.setStartOrReadyButton();	//设置按钮为开始或准备状态
		}
		
		//设置准备按钮
		public function RoomReadyUpButtonLoaderEvent(event:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.RoomReadyUpButtonIsLoader,RoomReadyUpButtonLoaderEvent);	//删除图片下载监听
			this.setStartOrReadyButton();	//设置按钮为开始或准备状态
		}
		
		//设置取消准备按钮(末做任何事)
		public function RoomUnReadyUpButtonLoaderEvent(event:ImageLoaderEvent):void{
			RoomSprite.imageLoader.removeEventListener(ImageLoaderEvent.RoomUnReadyUpButtonIsLoader,RoomUnReadyUpButtonLoaderEvent);	//删除图片下载监听
			this.setStartOrReadyButton();	//设置按钮为开始或准备状态
		}
		
		//设置按钮为开始或准备状态
		public function setStartOrReadyButton():void{			
			if(User.getPlayer().getUid()==User.getRoom().getMaster().getUid()){	//如果是房主
				this.startGameBtn.setStyle("icon",RoomSprite.imageLoader.startUpButton);	//显示开始图标
				startGameBtn.removeEventListener(MouseEvent.CLICK,ready);	//删除准备监听
				startGameBtn.addEventListener(MouseEvent.CLICK,startGame);	//添加点击开始按扭监听
			}else{
				if(User.getPlayer().getIsReady()){
					if(RoomSprite.imageLoader.unReadyUpButton!=null){
						this.startGameBtn.setStyle("icon",RoomSprite.imageLoader.unReadyUpButton);
					}
				}else{
					if(RoomSprite.imageLoader.readyUpButton!=null){
						this.startGameBtn.setStyle("icon",RoomSprite.imageLoader.readyUpButton);
					}
				}
				startGameBtn.removeEventListener(MouseEvent.CLICK,startGame);	//删除开始游戏监听
				startGameBtn.addEventListener(MouseEvent.CLICK,ready);	//添加准备监听
			}
		}
		
		
		//更新房间显示状态（按钮是否显示或可用等）
		public function refreshState():void{
			//trace(User.getPlayer().getUid()==User.getRoom().getMaster().getUid());
			//如果用户以满
			/*if(User.getRoom().getPlayers().length<8 && User.getPlayer().getUid()==User.getRoom().getMaster().getUid()){
				this.addChild(this.addComputerBtn);	//打开添加电脑按钮
			}else{
				this.addChild(this.addComputerBtn);	
				this.removeChild(this.addComputerBtn);
				
			}*/
			
			
			//打开开始或准备按钮
			startGameBtn.mouseEnabled=true;
			
		}
		
		
		//添加电脑按钮事件
		/*public function addComputerBtnClick(event:MouseEvent):void{
			//向服务器发送添加电脑玩家消息
			var ba:ByteArray=new ByteArray();
			ba.writeByte(15);	//消息类型
			ba.writeShort(0);	//消息内容的长度
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
			////房间电脑数+1
//			User.getRoom().setComputerCount(User.getRoom().getComputerCount()+1);
//			//创建电脑玩家
//			var player:Player=new Player();
//			player.setUserName("电脑");
//			player.setUid(0-User.getRoom().getComputerCount());
//			//将电脑玩家添加到房间并显示到房间界面
//			var seat:int=User.getRoom().addPlayer(player);
//			User.getRoomSprite().addPlayer(seat,player.getUserName());				
		}*/
		
		//打开添加电脑按钮
		/*public function addComputerBtnEnabled(bol:Boolean):void{
			this.addComputerBtn.mouseEnabled=bol;
		}*/
		
		//显示新加入的用户
		public function addPlayer(site:int,playerName:String):void{
			if(site>-1){
				ShowPlayerSprite(showPlayerSprites[site]).setPlayerName(playerName);
			}
			this.refreshState();	//更新房间状态（按钮是否显示或可用等）
		}
		
		
		
		//设置开始按钮是否接收用户操作
		public function startGameBtnEnabled(flag:Boolean):void{
			this.startGameBtn.mouseEnabled=flag;
		}
		
		//点击开始按钮
		private function startGame(event:MouseEvent):void
		{
			//如果房间内用户全部准备
			if(RoomBo.isAllReady()){
				//禁用开始或准备按钮
				this.startGameBtnEnabled(false);
				//向服务器请求开始游戏
				var ba:ByteArray=new ByteArray();
				ba.writeByte(7);	//消息类型
				ba.writeShort(0);	//消息内容的长度
				ProcessMsg.socketSend(ba);
				ba.clear();
				ba=null;
			}else{
				trace("有玩家末准备");
			}
		}
		
		//点击准备按钮
		private function ready(event:MouseEvent):void
		{
			RoomBo.sendReadyOrUnReadyMsg();
		}
		
		//点击退出房间按钮
		private function exitRoom(event:MouseEvent):void
		{
			//请求退出房间
			var ba:ByteArray=new ByteArray();
			ba.writeByte(8);	//消息类型
			ba.writeShort(0);	//消息内容的长度
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
		}
		
		//点击绿队按钮
		private function groupGreenBtnClickEvent(event:MouseEvent):void{
			var ba:ByteArray=new ByteArray();
			ba.writeByte(26);	//消息类型
			ba.writeShort(1);	//消息内容的长度
			ba.writeByte(1);	//消息的内容
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
		}
		//点击红队按钮
		private function groupRedBtnClickEvent(event:MouseEvent):void{
			var ba:ByteArray=new ByteArray();
			ba.writeByte(26);	//消息类型
			ba.writeShort(1);	//消息内容的长度
			ba.writeByte(0);	//消息的内容
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
		}
		//点击黄队按钮
		private function groupYellowBtnClickEvent(event:MouseEvent):void{
			var ba:ByteArray=new ByteArray();
			ba.writeByte(26);	//消息类型
			ba.writeShort(1);	//消息内容的长度
			ba.writeByte(2);	//消息的内容
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
		}
		//点击蓝队按钮
		private function groupBlueBtnClickEvent(event:MouseEvent):void{
			var ba:ByteArray=new ByteArray();
			ba.writeByte(26);	//消息类型
			ba.writeShort(1);	//消息内容的长度
			ba.writeByte(3);	//消息的内容
			ProcessMsg.socketSend(ba);
			ba.clear();
			ba=null;
		}
		

	}

}